const app = getApp()
import BaseModel from '../../utils/common.js'
var dateTimePicker = require('../../utils/dateTimePicker.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"",//早会id
    userOrder:"",//表单数据
    startYear: 2000, //开始年份
    endYear: 2050, //结束年份
    pickerDateTimeArray: null, //开始全部数组时间日期
    picker_time: null, //当前选中开始时间日期
    star_activity_time: "必填项",//活动开始时间
    end_activity_time: "必填项",//活动结束时间
    phone:"15036810684",//亚军电话
    is_subscribe:true,//是否上下架
    subscribe:"",//上下架值
    activityMes:"",//活动信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    this.setData({
      id:options.id,
      pickerDateTimeArray: obj.dateTimeArray,
      picker_time: obj.dateTime
    });
    this.getActivityDetailData(); //获取活动信息
  },
  /*开始日期*/
  changeStartTime(e) {
    var arr = this.data.picker_time,
        dateArr = this.data.pickerDateTimeArray;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    var star_activity_time= dateArr[0][arr[0]]+'-'+dateArr[1][arr[1]]+'-'+dateArr[2][arr[2]]+' '+dateArr[3][arr[3]]+':'+dateArr[4][arr[4]]
    this.setData({
      star_activity_time: star_activity_time
    });
  },
  /*结束时间*/
  changeEndTime:function(e){
    var arr = this.data.picker_time,
        dateArr = this.data.pickerDateTimeArray;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    var end_activity_time= dateArr[0][arr[0]]+'-'+dateArr[1][arr[1]]+'-'+dateArr[2][arr[2]]+' '+dateArr[3][arr[3]]+':'+dateArr[4][arr[4]]
    this.setData({
      end_activity_time: end_activity_time
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /*提交表单信息*/
  formSubmit: function(e) {
    if(e.detail.value.activity_name==""){
      wx.showToast({
        title: '课程名称未填写',
        icon: 'none'
      })
      return false;
    } else if (this.data.star_activity_time == "必填项") {
      wx.showToast({
        title: '课程开课时间',
        icon: 'none'
      })
      return false;
    } else if (this.data.end_activity_time == "必填项") {
      wx.showToast({
        title: '课程结束时间',
        icon: 'none'
      })
      return false;
    } else if (e.detail.value.address == "") {
      wx.showToast({
        title: '课程地址未填写',
        icon: 'none'
      })
      return false;
    } else if (e.detail.value.price == "") {
      wx.showToast({
        title: '课程价格未填写',
        icon: 'none'
      })
      return false;
    }
    this.setData({
      userOrder: e.detail.value
    })
    if(this.data.id){
      this.getEditActivityData();//修改
    }else{
      this.getActivityMesData();//提交
    }
  },
  //获取活动信息
  getActivityDetailModel:new BaseModel({
    url: "employees/ActivityDetail",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getActivityDetailData(){
    this.getActivityDetailModel
        .fetch({
          id:this.data.id
        })
        .then(res => {
          //成功
          this.setData({
            activityMes:res.data,
            star_activity_time: res.data.star_activity_time,//活动开始时间
            end_activity_time: res.data.end_activity_time,//活动结束时间
            is_subscribe:res.data.state
          })
        })
        .catch(res => {
          //失败 2018-12-20 00:00:00
        })
  },
  //提交信息
  getActivityMesModel:new BaseModel({
    url: "employees/addActivity",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getActivityMesData(){
    this.getActivityMesModel
        .fetch({
          activity_name:this.data.userOrder.activity_name,
          star_activity_time:this.data.star_activity_time,
          end_activity_time:this.data.end_activity_time,
          price:this.data.userOrder.price,
          address:this.data.userOrder.address
        })
        .then(res => {
          //成功
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 2000
          })
          wx.navigateBack({
            delta: 1
          })
        })
        .catch(res => {
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 2000
          })
          //失败 2018-12-20 00:00:00
        })
  },
  /*点击上下架*/
  clickSubscribe:function(){
    var that = this;
    that.setData({
      is_subscribe:!that.data.is_subscribe
    })
    if(that.data.is_subscribe){ //上架
      that.setData({
        subscribe:0,
      })
    }else{//下架
      that.setData({
        subscribe:1,
      })
    }
    that.getIsShieldData();
  },
  //上下架
  getIsShieldModel: new BaseModel({
    url: 'employees/unshelfActivitity',
    method: "get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getIsShieldData() {
    this.getIsShieldModel
        .fetch({
          "id":this.data.id,
          "state":this.data.subscribe
        })
        .then(res => {
          //成功
          wx.showToast({
            title: res.message,
            icon: 'success',
            duration: 2000
          })
        })
        .catch(res => {
          //失败
        })
  },
  //修改提交信息
  getEditActivityModel:new BaseModel({
    url: "employees/updateActivity",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getEditActivityData(){
    this.getEditActivityModel
        .fetch({
          id:this.data.id,
          activity_name:this.data.userOrder.activity_name,
          star_activity_time:this.data.star_activity_time,
          end_activity_time:this.data.end_activity_time,
          price:this.data.userOrder.price,
          address:this.data.userOrder.address
        })
        .then(res => {
          //成功
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 2000
          })
          wx.navigateBack({
            delta: 1
          })
        })
        .catch(res => {
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 2000
          })
          //失败 2018-12-20 00:00:00
        })
  },
  /*拨打电话*/
  phone:function(){
    wx.makePhoneCall({
      phoneNumber: this.data.phone //仅为示例，并非真实的电话号码
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh(); //停止下拉
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  /*onShareAppMessage: function () {

   }*/
})